<?php

namespace App\Console\Commands;

use App\Customer;
use App\Lead;
use Illuminate\Console\Command;

class LeadReplace extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'lead:chenhao';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       $customers= Customer::whereIn('project_id',[13,16])->get();
       $userIds=[41,45];
       foreach ($customers as $customer){
           if ($this->isPool($customer))  {
                    $lead= Lead::where('customer_id',$customer->customer_id)->first();
                    if ($lead) {
                        $newModel = $lead->replicate();
                        $newModel->owner_user_id = $userIds[array_rand($userIds)];
                        $newModel->create_user_id = 1;
                        $newModel->is_transform = 0;
                        $newModel->customer_id = 0;
                        $newModel->next_time = 0;
                        $newModel->follow = '待跟进';
                        $newModel->create_time = time();
                        $newModel->update_time = time();
                        $newModel->project_id = 23;
                        $newModel->status = '潜在客户';
                        $newModel->save();
                        $this->info($newModel->leads_id);
                    }
           }
       }



    }
    public function isPool(Customer $customer){
        if ($customer->is_lock==1){
            return false;
        }
        if ($customer->owner_user_id==0&&$customer->is_lock==0&&$customer->deal_status!='已成交'){
            return true;
        }
        if ($customer->update_time<=time() -30* 86400&&$customer->create_time<=strtotime('2020-08-01 00:00:00')){
            return true;
        }
        if ($customer->update_time<=time() -30* 86400&&$customer->create_time>strtotime('2020-08-01 00:00:00')){
            return true;
        }
        if ($customer->deal_time<=time()-60*86400&&$customer->create_time<=strtotime('2020-08-01 00:00:00')){
            return true;
        }
        if ($customer->deal_time<=time()-60*86400&&$customer->create_time>strtotime('2020-08-01 00:00:00')){
            return true;
        }
        return false;
    }
}
